package psn.kiko.service.business.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import psn.kiko.constant.MessageConstant;
import psn.kiko.excepion.BaseBusinessException;
import psn.kiko.mapper.business.system.IconFontMapper;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.dto.IconFontDTO;
import psn.kiko.pojo.entity.system.IconFontEntity;
import psn.kiko.pojo.vo.IconFontVO;
import psn.kiko.pojo.vo.SimpleIconFontVO;

import java.util.List;

/**
 * 字体图标服务
 */
@Service
public class IconFontService{
	@Autowired
	IconFontMapper iconFontMapper;
	
	/**
	 * 分页查询
	 */
	public PageResult getIconFonts(IconFontDTO iconFontDTO,int pageNUm,int pageSize){
		Page<Object> page = PageHelper.startPage(pageNUm,pageSize);
		IconFontEntity iconFontEntity = new IconFontEntity();
		BeanUtils.copyProperties(iconFontDTO,iconFontEntity);
		
		List<IconFontVO> iconFontVOS = iconFontMapper.selectList(iconFontEntity);
		
		return PageResult.builder()
				.total(((int) page.getTotal()))
				.data(iconFontVOS)
				.build();
	}
	
	/**
	 * 新增
	 */
	public Integer addIconFont(IconFontDTO iconFontDTO){
		Integer insertCount = 0;
		try{
			IconFontEntity iconFontEntity = new IconFontEntity();
			BeanUtils.copyProperties(iconFontDTO,iconFontEntity);
			insertCount = iconFontMapper.insert(iconFontEntity);
		}catch(Exception e){
			e.printStackTrace();
			if(e instanceof DuplicateKeyException){
				throw new BaseBusinessException(MessageConstant.INSERT_DUPLICATE_ROWS);
			}else
				throw new BaseBusinessException(MessageConstant.UNKNOWN_ERROR);
		}
		return insertCount;
	}
	
	/**
	 * 修改
	 */
	public Integer editIconFont(IconFontDTO iconFontDTO){
		IconFontEntity iconFontEntity = new IconFontEntity();
		BeanUtils.copyProperties(iconFontDTO,iconFontEntity);
		return iconFontMapper.updateById(iconFontEntity);
	}
	
	/**
	 * 根据主键批量删除
	 */
	public Integer deleteIconFonts(List<Integer> iconFontIds){
		return iconFontMapper.deleteByIds(iconFontIds);
	}
	
	public IconFontVO getIconFont(int id){
		return iconFontMapper.selectById(id);
	}
	
	/**
	 * 获取简易图标VO选项
	 */
	public List<SimpleIconFontVO> getSimpleIconFont(){
		return iconFontMapper.selectSimpleList();
	}
}
